package com.example.demo.internettongxin.tcptongxin2;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Description T2server
 * @Author songchuanlong
 * @Date 2025-05-30
 */
public class T2server {
    public static void main(String[] args) throws Exception {
        //创建ServerSocket对象，同时为服务端注册端口
        ServerSocket serverSocket = new ServerSocket(8888);
        //2、使用serverSocket对象，调用一个accept方法，等待客户端连接
        Socket socket = serverSocket.accept();
        //3、从socket通信管道中获得一个字节输入流
        InputStream is = socket.getInputStream();
        //4、把原始字节输入流包装成数据输入流
        DataInputStream dis = new DataInputStream(is);
        while (true) {
            try {
                String rs = dis.readUTF();
                System.out.println(rs);
            } catch (Exception e) {
                System.out.println(socket.getRemoteSocketAddress()+"离线");
                dis.close();
                socket.close();
                break;
            }
        }
    }
}
